package cn.gemiman.daoyi.module.iot.controller.app.userequipment;

import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.security.core.annotations.PreAuthenticated;
import cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentdevice.vo.JhlEquipmentDeviceSaveReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipment.vo.UserEquipmentBindReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipment.vo.UserEquipmentPageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipment.vo.UserEquipmentRespVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipment.vo.UserEquipmentSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.userequipment.UserEquipmentDO;
import cn.gemiman.daoyi.module.iot.service.jhlequipmentdevice.JhlEquipmentDeviceService;
import cn.gemiman.daoyi.module.iot.service.userequipment.UserEquipmentService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 会员设备")
@RestController
@RequestMapping("/iot/user-equipment")
@Validated
public class AppUserEquipmentController {

    @Resource
    private UserEquipmentService userEquipmentService;
    @Resource
    private JhlEquipmentDeviceService jhlEquipmentDeviceService;

    @PostMapping("/create")
    @Operation(summary = "绑定会员设备")
    @PreAuthenticated
    public CommonResult<Long> createUserEquipment(@Valid @RequestBody UserEquipmentBindReqVO bindReqVO) {
        Integer id = jhlEquipmentDeviceService.createJhlEquipmentDevice(new JhlEquipmentDeviceSaveReqVO()
                .setDeviceIdentifier(bindReqVO.getDeviceIdentifier())
                .setDeviceName(bindReqVO.getDeviceName())
        );
        UserEquipmentSaveReqVO createReqVO = BeanUtil.copyProperties(bindReqVO, UserEquipmentSaveReqVO.class)
                .setEquipmentId(Long.valueOf(id)).setMemberUserId(getLoginUserId());
        return success(userEquipmentService.createUserEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员设备")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserEquipment(@Valid @RequestBody UserEquipmentBindReqVO bindReqVO) {
        Integer id = jhlEquipmentDeviceService.createJhlEquipmentDevice(new JhlEquipmentDeviceSaveReqVO()
                .setDeviceIdentifier(bindReqVO.getDeviceIdentifier())
                .setDeviceName(bindReqVO.getDeviceName())
        );
        UserEquipmentSaveReqVO createReqVO = BeanUtil.copyProperties(bindReqVO, UserEquipmentSaveReqVO.class)
                .setEquipmentId(Long.valueOf(id)).setMemberUserId(getLoginUserId());
        userEquipmentService.updateUserEquipment(createReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserEquipment(@RequestParam("id") Long id) {
        userEquipmentService.deleteUserEquipment(id, getLoginUserId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<UserEquipmentRespVO> getUserEquipment(@RequestParam("id") Long id) {
        UserEquipmentDO userEquipment = userEquipmentService.getUserEquipment(id);
        if (!Objects.requireNonNull(getLoginUserId()).equals(userEquipment.getMemberUserId())) {
            userEquipment = null;
        }
        return success(BeanUtils.toBean(userEquipment, UserEquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员设备分页")
    @PreAuthenticated
    public CommonResult<PageResult<UserEquipmentRespVO>> getUserEquipmentPage(@Valid UserEquipmentPageReqVO pageReqVO) {
        pageReqVO.setMemberUserId(getLoginUserId());
        PageResult<UserEquipmentDO> pageResult = userEquipmentService.getUserEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserEquipmentRespVO.class));
    }

}
